package com.jtech.together.view.weight;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtech.together.R;
import com.jtech.together.util.DeviceUtils;
import com.jtech.together.util.EmojiUtil;
import com.jtech.together.view.adapter.EmojiAdapter;
import com.jtech.together.view.adapter.EmojiPagerAdapter;
import com.jtechlib2.listener.OnItemClickListener;
import com.jtechlib2.view.JRecyclerView;
import com.jtechlib2.view.RecyclerHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 表情键盘
 * Created by wuxubaiyang on 2017/5/15.
 */

public class EmojiKeyboard extends CardView implements ViewPager.OnPageChangeListener {
    @Bind(R.id.viewpager_emoji_keyboard)
    ViewPager viewPager;
    @Bind(R.id.linearlayout_emoji_keyboard)
    LinearLayout linearLayoutDots;

    private OnEmojikeyboardListener onEmojikeyboardListener;
    private List<ImageView> dots;
    private TextView targetInput;

    public EmojiKeyboard(Context context) {
        this(context, null);
    }

    public EmojiKeyboard(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmojiKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化视图
        init();
    }

    /**
     * 初始化视图
     */
    private void init() {
        //实例化视图
        View contentView = LayoutInflater.from(getContext())
                .inflate(R.layout.view_emoji_keyboard, this, false);
        //绑定视图
        ButterKnife.bind(this, contentView);
        //添加视图
        addView(contentView);
        //设置卡片
        this.setCardBackgroundColor(Color.WHITE);
        //添加卡片切换监听
        viewPager.addOnPageChangeListener(this);
        //获取表情集合
        List<int[]> emojis = EmojiUtil.getEmojiList();
        //初始化表情集合
        initPager(emojis);
    }

    /**
     * 初始化表情集合
     *
     * @param emojis
     */
    private void initPager(List<int[]> emojis) {
        this.dots = new ArrayList<>();
        List<JRecyclerView> pagers = new ArrayList<>();
        //遍历卡片集合
        int dotSize = DeviceUtils.dip2px(getContext(), 5);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(dotSize, dotSize);
        for (int i = 0; i < emojis.size(); i++) {
            ImageView dot = new ImageView(getContext());
            dot.setLayoutParams(layoutParams);
            linearLayoutDots.addView(dot);
            dots.add(dot);
            View view = new View(getContext());
            view.setLayoutParams(new LinearLayout.LayoutParams(DeviceUtils.dip2px(getContext(), 4), 0));
            linearLayoutDots.addView(view);
            //实例化表情列表
            JRecyclerView jrecyclerView = new JRecyclerView(getContext());
            jrecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 7));
            final EmojiAdapter adapter = new EmojiAdapter(getContext());
            jrecyclerView.setAdapter(adapter);
            adapter.setDatas(emojis.get(i));
            jrecyclerView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerHolder holder, View view, int position) {
                    setEmoji(adapter.getItem(position));
                }
            });
            pagers.add(jrecyclerView);
        }
        viewPager.setAdapter(new EmojiPagerAdapter(pagers));
        //默认选择首位
        dotSelect(0);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (null != onEmojikeyboardListener) {
            onEmojikeyboardListener.onEmojiVisibilityChange(visibility == VISIBLE);
        }
    }

    /**
     * 与文本框绑定
     *
     * @param editText
     */
    public void setupTargetInput(TextView editText) {
        this.targetInput = editText;
    }

    /**
     * 设置表情键盘监听
     *
     * @param onEmojikeyboardListener
     */
    public void setOnEmojikeyboardListener(OnEmojikeyboardListener onEmojikeyboardListener) {
        this.onEmojikeyboardListener = onEmojikeyboardListener;
    }

    /**
     * 设置表情
     *
     * @param emoji
     */
    private void setEmoji(String emoji) {
        if (null != targetInput) {
            Editable editable = targetInput.getEditableText();
            if (targetInput.getSelectionStart() != targetInput.getSelectionEnd()) {
                editable.delete(targetInput.getSelectionStart(), targetInput.getSelectionEnd());
            }
            editable.insert(targetInput.getSelectionStart(), emoji);
        }
    }

    /**
     * 点选择
     *
     * @param index
     */
    private void dotSelect(int index) {
        for (int i = 0; i < dots.size(); i++) {
            dots.get(i).setImageResource(index == i ?
                    R.drawable.button_dot_selected : R.drawable.button_dot_normal);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        dotSelect(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * 表情键盘监听
     */
    public interface OnEmojikeyboardListener {
        void onEmojiVisibilityChange(boolean isVisibility);
    }
}